package com.fin.zw.aiqas.controller;


import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.SystemParam;
import com.fin.zw.aiqas.model.request.SystemParamRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.SystemParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/systemParam")
@Api(value = "系统参数管理", description = "系统参数管理")
public class SystemParamController {

    @Autowired
    private SystemParamService systemParamService;

    @GetMapping("/fetchPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<SystemParam>> querySystemParam(Pageable pageable,
                                                              SystemParamRequest request) {
        Page<SystemParam> page = systemParamService.querySystemParam(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    @PostMapping("/add")
    @ApiModelProperty(value = "系统参数的添加", notes = "系统参数的添加")
    public ResponseEntity<Void> createSystemParam(@RequestBody SystemParamRequest request,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) {
        systemParamService.createSystemParam(request, sessionModel);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/update")
    @ApiModelProperty(value = "系统参数的更新", notes = "系统参数的更新")
    public ResponseEntity<Void> updateSystemParam(@RequestBody SystemParamRequest request) {
        systemParamService.updateSystemParam(request);
        return ResponseEntity.ok().build();
    }


    @DeleteMapping("/delete/{id}")
    @ApiModelProperty(value = "系统参数的删除", notes = "系统参数的删除")
    public ResponseEntity<Void> deleteSystemParam(@PathVariable Long id) {
        systemParamService.deleteById(id);
        return ResponseEntity.ok().build();
    }
}
